<template>
  <div class="container">
    <el-card shadow="never">
      <!-- 搜索 -->
      <el-form size="mini" label-width="80px">
        <el-row>
          <el-col :span="6">
            <el-form-item label="关键字">
              <el-input
                @keyup.enter="getRandomsKeyword"
                placeholder="根据编号搜索"
                v-model="keyword"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="18" style="text-align: right">
            <el-button size="small" @click="clear">清除</el-button>
            <el-button size="small" type="primary" @click="getRandomsKeyword"
              >搜索</el-button
            >
          </el-col>
        </el-row>
      </el-form>
      <el-alert
        :title="`数据一共 ${count} 条`"
        type="info"
        class="alert"
        :closable="false"
        show-icon
      ></el-alert>
      <el-table
        size="small"
        :data="data"
        v-loading="loading"
        element-loading-text="给我一点时间"
        fit
        highlight-current-row
        style="width: 100%"
      >
        <el-table-column prop="id" label="编号" width="180"> </el-table-column>
        <el-table-column
          prop="questionType"
          label="题型"
          align="center"
          width="150"
          :formatter="formatType"
        >
        </el-table-column>
        <el-table-column prop="questionIDs" label="题目编号" width="220">
          <template slot-scope="{ row }">
            <div v-for="(item, index) in row.questionIDs" :key="index">
              <div @click="getPreview(item.id)">
                <a href="#" style="color: dodgerblue">{{ item.number }}</a>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="addTime" label="录入时间(s)" width="180">
        </el-table-column>
        <el-table-column prop="totalSeconds" label="答题时间(s)">
        </el-table-column>
        <el-table-column prop="accuracyRate" label="正确率(%)">
        </el-table-column>
        <el-table-column prop="userName" label="录入人"></el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="{ row }">
            <el-button
              type="danger"
              size="mini"
              plain
              circle
              icon="el-icon-delete"
              @click="del(row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="pagination">
        <PageTool
          :page="query.page"
          :pagesize="query.pagesize"
          :total="count"
          @pageChange="handleCurrentChange"
          @pageSizeChange="handleSizeChange"
        ></PageTool>
      </div>
    </el-card>
    // 弹层
    <Dialog
      :dialog.sync="dialog"
      @close="dialog = false"
      :questionData="questionData"
    />
  </div>
</template>

<script>
import PageTool from '../components/random-page-tool.vue'
import Dialog from '../components/random-dialog.vue'
import { getRandoms, delRandoms, getPreview } from '@/api/hmmm/questions'
import { questionType } from '@/api/hmmm/constants'

export default {
  name: 'QuestionRandoms',
  components: {
    PageTool,
    Dialog
  },
  data () {
    return {
      count: 0,
      data: [],
      query: {
        page: 1,
        pagesize: 20
      },
      keyword: '',
      loading: false,
      dialog: false,
      questionData: {}
    }
  },
  created () {
    this.getRandoms()
  },
  methods: {
    // 获取题组列表数据
    async getRandoms () {
      this.loading = true
      const { data } = await getRandoms(this.query)
      // console.log(data)
      this.count = data.counts
      this.data = data.items
      // console.log(this.data)
      this.loading = false
    },
    // 每页显示信息条数
    handleSizeChange (val) {
      this.query.pagesize = val
      this.getRandoms()
    },
    // 进入某一页
    handleCurrentChange (val) {
      this.query.page = val
      this.getRandoms()
    },
    // 处理 题型 数据
    formatType (row, column, cellValue, index) {
      for (let i = 0; i < questionType.length; i++) {
        // console.log(questionType[i].value)
        // console.log(cellValue)
        if (questionType[i].value === +cellValue) {
          return questionType[i].label
        }
      }
    },
    // 题库预览
    async getPreview (id) {
      try {
        this.dialog = true
        const { data } = await getPreview({ id })
        console.log(data)
        this.questionData = data
      } catch (error) {
        this.$message.info('数据未找到')
        return error
      }
    },
    // 根据编号 查找题组
    async getRandomsKeyword () {
      try {
        this.loading = true
        const { data } = await getRandoms({
          page: 1,
          pagesize: 20,
          keyword: this.keyword
        })
        // console.log(data)
        this.count = data.counts
        this.data = data.items
        this.loading = false
      } catch (error) {}
    },
    // 清空输入框
    clear () {
      this.keyword = ''
      this.query.page = 1
      this.getRandoms()
    },
    // 删除题组
    async del (obj) {
      await this.$confirm('此操作将永久删除该题组, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      await delRandoms(obj)
      const { data } = await getRandoms(this.query)
      // console.log(data)
      // console.log(this.data)
      if (data.items.length === 0 && this.query.page > 1) {
        this.query.page--
        this.getRandoms()
      } else {
        this.count = data.counts
        this.data = data.items
      }
    }
  }
}
</script>

<style scoped>
.alert {
  margin: 10px 0px;
  color: rgba(10, 100, 203, 0.329);
}
</style>
